﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataBaseCommon;
using NetCore6WebAPITestOneEntity;
using CommonEntity;
using NetCore6WebAPITestOneRequestEntity;
using ConfigurationCommon;

namespace NetCroe6WebAPITestOneResponse
{
    public class UserinfoResponse
    {
        ConfigurationScopeHelper _configurationScopeHelper;

        public UserinfoResponse(ConfigurationScopeHelper configurationScopeHelper)
        {
            this._configurationScopeHelper = configurationScopeHelper;
        }


        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserinfoModel GetUserModel(long userId)
        {
            var db = SqlSugarClientScopedHelper.GetMySqlDbStatic(_configurationScopeHelper._userMysqlConnectionString);
            return db.Queryable<UserinfoModel>().Where(it => it.UserID == userId).ToList().FirstOrDefault();
        }

        public UserinfoModel GetUserModel(UserLoginViewModel userinfo)
        {
            var db = SqlSugarClientScopedHelper.GetMySqlDbStatic(_configurationScopeHelper._userMysqlConnectionString);
            return db.Queryable<UserinfoModel>().Where(it => it.UserName == userinfo.UserName&&it.UserPwd==userinfo.UserPwd).ToList().FirstOrDefault();
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public PageViewModel<UserinfoModel> GetUsers(int pageindex,int pagesize) {
            PageViewModel<UserinfoModel> page = new PageViewModel<UserinfoModel>();
            int totalCount = 0;
            var db = SqlSugarClientScopedHelper.GetMySqlDbStatic(_configurationScopeHelper._userMysqlConnectionString);
            page.PageList = db.Queryable<UserinfoModel>().ToPageList(pageindex, pagesize, ref totalCount);
            return page;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool InserUserinfo(UserinfoModel user) {
            var db = SqlSugarClientScopedHelper.GetMySqlDbStatic(_configurationScopeHelper._userMysqlConnectionString);
            return db.Insertable(user).ExecuteCommand() == 1;
        }
    }
}
